package com.mrd.mina;

import com.mrd.game.message.HOpCodeEx;
import com.mrd.game.message.RainbowMessage.CGBet;
import com.mrd.packet.Packet;
import com.yuerlive.rainbow.nim.NimHelper;
import com.yuerlive.rainbow.utils.L;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

/**
 * 客户端业务处理逻辑
 *
 * @author aniyo blog: http://aniyo.iteye.com
 */
public class MinaClientHandler extends IoHandlerAdapter {
    // 当客户端连接进入时
    @Override
    public void sessionOpened(IoSession session) throws Exception {
        /*System.out.println("incomming 客户端: " + session.getRemoteAddress());

        CGBet.Builder builder = CGBet.newBuilder();
        builder.setUserId(9999);
        builder.setChessboardId(2001);
        builder.setGameId(1);
        builder.setIndex(3);
        builder.setValue(300);
        builder.setToken("我的token123456");
        Packet packet = Packet.newPbPacket(HOpCodeEx.CGBet, builder.build().toByteArray());
        packet.setFlag(9);
        packet.setStamp(System.currentTimeMillis());
        packet.setRetCode(0);


        session.write(packet);*/

        MinaHelper.getInstance().onConnected(session);
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause)
            throws Exception {
        System.out.println("客户端发送信息异常....");
        MinaHelper.getInstance().onException(session, cause);
    }

    // 当客户端发送消息到达时
    @Override
    public void messageReceived(IoSession session, Object message)
            throws Exception {

        System.out.println("服务器返回的数据：" + message.toString());
        MinaHelper.getInstance().onReceiveMessage(session, message);
        Packet packet = (Packet) message;
        L.e("。。。", "data   " + packet.getData());
    }

    @Override
    public void sessionClosed(IoSession session) throws Exception {
        System.out.println("客户端与服务端断开连接.....");
        MinaHelper.getInstance().onDisconnected(session);
    }

    @Override
    public void sessionCreated(IoSession session) throws Exception {
        // TODO Auto-generated method stub
    }


}
